<template>
  <div class="app-container">
    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['system:config:add']"
        >新增</el-button>
      </el-col>
    </el-row>

    <el-table v-loading="loading" :data="configList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="系统属性KEY" align="center" prop="attr_key" />
      <el-table-column label="系统属性提示" align="center" prop="attr_tip" />
      <el-table-column label="系统属性值" align="center" prop="attr_value" />
      <el-table-column label="字典属性组KEY" align="center" prop="group_key" />
      <el-table-column label="添加操作人" align="center" prop="save_operator" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['system:config:edit']"
          >修改</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改系统属性对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="800px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="180px">
        <el-form-item label="Key" prop="attrKey">
          <el-input v-if="title === '添加系统属性'" v-model="form.attrKey" placeholder="请输入系统属性提示" />
          <el-input v-else readonly v-model="form.attrKey" placeholder="请输入系统属性提示" />
        </el-form-item>
        <el-form-item label="系统属性提示" prop="attrTip">
          <el-input v-model="form.attrTip" placeholder="请输入系统属性提示" />
        </el-form-item>
        <el-form-item label="Value" prop="attrValue">
          <el-input v-model="form.attrValue" placeholder="请输入系统属性值" />
        </el-form-item>
        <el-form-item label="分组" prop="groupKey">
          <el-input v-model="form.groupKey" placeholder="请输入字典属性组KEY" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listConfig, getConfig, addConfig, updateConfig } from "@/api/system/config";

export default {
  name: "Config",
  components: {
  },
  data() {
    let validateAttrKey = (rule, value, callback) => {
      if (this.title !== '添加系统属性') {
        getConfig(value).then(response => {
          if (response.data) {
            return callback(new Error("当前Key已存在"))
          }
          callback();
        })
      }

      callback();
    }
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 系统属性表格数据
      configList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        attrTip: null,
        attrValue: null,
        groupKey: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        attrKey: [
          { required: true, message: "Key不能为空", trigger: "blur" },
          { required: true, trigger: "blur", validator: validateAttrKey}
        ],
        attrValue: [
          { required: true, message: "Value不能为空", trigger: "blur" }
        ],
        groupKey: [
          { required: true, message: "分组不能为空", trigger: "blur" }
        ],
        attrTip: [
          { required: true, message: "Tip不能为空", trigger: "blur" },
        ]
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询系统属性列表 */
    getList() {
      this.loading = true;
      listConfig(this.queryParams).then(response => {
        this.configList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        attr_key: null,
        attr_tip: null,
        attr_value: null,
        group_key: null
      };
      this.resetForm("form");
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.attrKey)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加系统属性";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const attrKey = row.attr_key || this.ids

      getConfig(attrKey).then(response => {
        this.form = {
          attrKey: response.data.attr_key,
          attrTip: response.data.attr_tip,
          attrValue: response.data.attr_value,
          groupKey: response.data.group_key,
        };
        this.open = true;
        this.title = "修改系统属性";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.title !== "添加系统属性") {
            this.form.save_operator = null
            this.form.update_operator = null
            updateConfig(this.form).then(response => {
              this.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addConfig(this.form).then(response => {
              this.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    }
  }
};
</script>
